<template>
	<view class="setting-container" :style="{height: `${windowHeight}px`}">
		<view class="menu-list">
			<view class="classWrap" v-for="(item,index) in list" :key="index">
				<view class="left">
					{{item.remark}}
				</view>
				<view class="right">
					<u-switch size="20" v-model="item.active" @change="(val)=>activeChange(val, item)"></u-switch>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getDictType,
		configMessage,
		deleteConfigMessage
	} from '@/api/system/public'
	import {
		configMessageList
	} from '@/api/personalCenter/index.js'
	export default {
		data() {
			return {
				windowHeight: uni.getSystemInfoSync().windowHeight,
				list: [],
				removeList: [],
				openid: '',
			}
		},
		onLoad() {
			let openid = uni.getStorageSync('openid') || ''
			this.openid = openid
			this.getList()
		},
		methods: {
			getList() {
				getDictType({
					dictType: 'wechat_message_type'
				}).then(res => {
					console.log(res)
					if (res.code == 200) {
						res.rows.map(item => {
							item.active = true
						})
						let dataList = res.rows
						configMessageList({
							openId: this.openid
						}).then(ress => {
							console.log(ress)
							this.removeList = ress.rows
							ress.rows.forEach(ite => {
								dataList.forEach(items => {
									if (ite.msgType == items.dictLabel) {
										items.active = false
									}
								})
							})

							this.list = dataList
						})
					}
				})
			},
			activeChange(val, item) {
				let parms = {
					hospitalCode: item.hospitalCode,
					openId: this.openid,
					msgType: item.dictLabel,
				}
				if (val == false) {
					configMessage(parms).then(res => {
						if (res.code == 200) {
							this.$modal.showToast('修改成功')
							this.getList()
						}
					})
				} else {
					let index = this.removeList.findIndex(items => items.msgType == item.dictLabel)
					if (index != -1) {
						deleteConfigMessage(this.removeList[index].id).then(res => {
							if (res.code == 200) {
								this.$modal.showToast('修改成功')
								this.getList()
							}
						})
					}

				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background-color: #f8f8f8;
	}

	.menu-list {
		padding: 10px;

		.classWrap {
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1rpx solid #d1d1d1;
			padding: 10px 0;

			.left {
				font-size: 1rem;
			}
		}
	}
</style>